package com.fuerda.izb.view

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import com.blankj.utilcode.util.ScreenUtils
import java.util.*
import kotlin.math.abs


class MoveQuatoView : View {
    private var mPaint: Paint? = null
    private var mPath: Path? = null

    //上个位置
    private var mPreX = 0f
    private var mPreY = 0f

    //结束位置
    private var endY = 0f
    private var endX = 0f

    //波纹的宽度
    private var startX = 800f
    private var startX1 = 800f

    private var tStartX = 600f
    private var tStartX1 = 600f
    private var startY = 300f
    private var startY1 = 300f
    private var moveY = 0f
    private var moveY1 = 0f
    private var moveX = 0f
    private var HorizonmMoveX = 0f
    private var HorizonmMoveX1 = 0f
    private var eventY = 0f
    private var eventX = 0f
    private var dx = 0f
    var tx1 = -100f
    var tx3 = -100f
    var y1 = -80f
    var y2 = 100f
    var y3 = -80f
    var y4 = 100f
    private var mYRadius = 100f
    var mXRadius = 72f
    var mCenter1 = 0f
    var mCenter2 = 0f
    var mWidth = ScreenUtils.getScreenWidth()
    var mHight = ScreenUtils.getScreenHeight()
    var strokeWidth = 4f
    var rainNum = 26
    var widthper = 0.5

    constructor(context: Context?) : super(context) {
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
            context,
            attrs,
            defStyleAttr
    ) {
    }

    //初始化
    private fun init() {
        setBackgroundColor(Color.TRANSPARENT)

        mPath = Path()
        mPaint = Paint()
        mPaint!!.color = Color.WHITE
        mPaint!!.strokeWidth = strokeWidth
        mPaint!!.alpha = 30
        mPaint!!.style = Paint.Style.STROKE
        mPaint!!.isAntiAlias = true
        mPaint!!.setShadowLayer(8f, 8f, 7f, Color.parseColor("#f0f0f0"))
        mPaint!!.strokeJoin = Paint.Join.ROUND
        mPaint!!.strokeCap = Paint.Cap.ROUND
        val lighting = LightingColorFilter(0xffffff, 0x2B2B2D)
        mPaint!!.colorFilter = lighting
        mPaint!!.maskFilter = BlurMaskFilter(5f, BlurMaskFilter.Blur.OUTER)
        mCenter1 = (mWidth / 2).toFloat() + mXRadius * 2
        mCenter2 = (mWidth / 2).toFloat() - mXRadius * 2
        startX = mCenter1 + mXRadius / 6
        startX1 = mCenter2 - mXRadius / 6
        startY = (mHight * 2/ 5).toFloat()
        startY1 = (mHight * 2/ 5).toFloat()
        startAnim()
    }
    private val vmax = 20
    private val vmin = -20

    private val tmax = 5
    private val tmin = -5
    var ty1 = 0f
    var ty2 = 0f
    var ty3 = 0f
    var ty4 = 0f
    private val mColors1 = intArrayOf(Color.parseColor("#99FFFFFF"), Color.parseColor("#002B2B2D"))

    private val mColors = intArrayOf(Color.parseColor("#F5FFFFFF"), Color.parseColor("#002B2B2D"))
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        mPaint!!.setMaskFilter(BlurMaskFilter(5f, BlurMaskFilter.Blur.NORMAL)) //设置发光
        canvas.drawFilter = PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG)
        for (j in 0..6) {
            var i = 0
            while (i < rainNum) {
                val random = Random()
                val vR = random.nextInt(vmax) % (vmax - vmin + 1) + vmin
                val rad = random.nextInt(tmax) % (tmax - tmin + 1) + tmin
                ty1 = y1 - moveY + j * 4f
                ty3 = y3 - moveY1 + j * 4f
                //移动右边的
                tStartX = (startX + HorizonmMoveX + i * strokeWidth * widthper).toFloat()
                tx1 = tStartX - mCenter1
                val shader1: Shader = LinearGradient(tStartX + tx1 + rad, startY + ty1, tStartX + 5 * tx1 + rad, startY - ty1 * 2, if ((mPreX >= mWidth / 2 && isMove == true)) mColors else mColors1, null, Shader.TileMode.MIRROR) //渐变处理
                mPath!!.moveTo(tStartX, startY)
                mPaint!!.shader = shader1
                mPath!!.rQuadTo(tx1, ty1 + vR, 2 * tx1 + rad, -ty1 / 4 + vR)
                mPath!!.rQuadTo(tx1 + rad, -ty1 + vR, 2 * tx1 + rad, -ty1 + vR)
                canvas.drawPath(mPath!!, mPaint!!)
                mPath!!.reset()

                val vR1 = random.nextInt(vmax) % (vmax - vmin + 1) + vmin
                tStartX1 = (startX1 + HorizonmMoveX1 - i * strokeWidth * widthper).toFloat()
                tx3 = (tStartX1 - mCenter2)

                val shader2: Shader = LinearGradient(tStartX1 + tx3 + rad, startY1 + ty1, tStartX1 + 5 * tx3 + rad, startY1 - ty3 * 2, if ((mPreX <= mWidth / 2 && isMove == true)) mColors else mColors1, null, Shader.TileMode.MIRROR) //渐变处理
                mPath!!.moveTo(tStartX1, startY)
                mPaint!!.shader = shader2
                mPath!!.rQuadTo(tx3, ty3 + vR1, 2 * tx3 + rad, -ty3 / 4 + vR1)
                mPath!!.rQuadTo(tx3 + rad, -ty3 + vR1, 2 * tx3 + rad, -ty3 + vR1)
                canvas.drawPath(mPath!!, mPaint!!)
                mPath!!.reset()
                i = i + 1

            }
        }
    }

    fun setPercent(){
        var per:Float=0.8f
        startX1=mCenter2+mXRadius*2*per
        invalidate()
    }
    fun directionfinish() {
        startX = HorizonmMoveX + startX
        startX1 = startX1 + HorizonmMoveX1
        HorizonmMoveX = 0f
        HorizonmMoveX1 = 0f
        y1 = y1 - moveY
        y3 = y3 - moveY1
        moveY = 0f
        moveY1 = 0f
    }
    public fun directionChange(tag: Int, touchCount:Int) {
        var xmove =touchCount* mXRadius / 15
        var ymove = touchCount*mYRadius / 30
        when (tag) {
            1 -> {
                if (Math.abs(startX1 - xmove - rainNum * strokeWidth * widthper - mCenter2) <= mXRadius) {
                    HorizonmMoveX1 = -xmove
                } else {
                    HorizonmMoveX1 = (mCenter2 - mXRadius - startX1 + rainNum * strokeWidth * widthper).toFloat()
                }
            }
            2 -> {
                ymove=-ymove
                if (Math.abs((y3 - ymove)) < mYRadius) {
                    moveY1 = ymove
                } else {
                        moveY1 = -mYRadius + y3
                }
            }
            3 -> {
                if (Math.abs((y3 - ymove)) < mYRadius) {
                    moveY1 = ymove
                } else {
                   moveY1 = mYRadius + y3
                }
            }
            4 -> {
                if (Math.abs(startX1 + xmove - mCenter2) <= mXRadius) {
                    HorizonmMoveX1 = xmove
                } else {
                    HorizonmMoveX1 = mCenter2 + mXRadius - startX1
                }
            }
            9 -> {
                if (Math.abs(startX - xmove - mCenter1) <= mXRadius) {
                    HorizonmMoveX = -xmove
                } else {
                    HorizonmMoveX = mCenter1 - mXRadius - startX
                }
            }
            10 -> {
                ymove=-ymove
                if (Math.abs((y1 - ymove)) < mYRadius) {
                    moveY = ymove
                } else {
                    moveY = -mYRadius + y1
                }
            }
            11 -> {
                if (Math.abs((y1 - ymove)) < mYRadius) {
                    moveY = ymove
                } else {
                    moveY = mYRadius + y1
                }
            }
            12 -> {
                if (Math.abs(startX + xmove + rainNum * strokeWidth * widthper - mCenter1) <= mXRadius) {
                    HorizonmMoveX = xmove
                } else {
                    HorizonmMoveX = (mXRadius + mCenter1 - rainNum * strokeWidth * widthper - startX).toFloat()
                }
            }
        }
        invalidate()
    }

    var task: TimerTask? = null
    fun startAnim() {
        task = object : TimerTask() {
            override fun run() {
                val random = Random()
                if (isMove == false) {
                    dx = (random.nextInt(vmax) % (vmax - vmin + 1) + vmin).toFloat()
                    postInvalidate()
                }
            }
        }
        val timer = Timer()
        val delay: Long = 0
        val intevalPeriod: Long = 1000
        timer.scheduleAtFixedRate(task, delay, intevalPeriod)
    }

    private var isMove = false
    override fun onTouchEvent(event: MotionEvent): Boolean {
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                mPreX = event.x
                mPreY = event.y
            }
            MotionEvent.ACTION_MOVE -> {
                var xmove = (event.x - mPreX) / 4
                eventX = event.x
                if (mPreX > mWidth / 2) {
                    HorizonmMoveX = if (xmove > 0) {
                        if (Math.abs(startX + xmove + rainNum * strokeWidth * widthper - mCenter1) <= mXRadius) {
                            xmove
                        } else {
                            (mXRadius + mCenter1 - rainNum * strokeWidth * widthper - startX).toFloat()
                        }
                    } else {
                        if (Math.abs(startX + xmove - mCenter1) <= mXRadius) {
                            xmove
                        } else {
                            mCenter1 - mXRadius - startX
                        }
                    }

                } else {
                    HorizonmMoveX1 = if (xmove > 0) {
                        if (abs(startX1 + xmove - mCenter2) <= mXRadius) {
                            xmove
                        } else {

                            mCenter2 + mXRadius - startX1
                        }
                    } else {
                        if (Math.abs(startX1 + xmove - rainNum * strokeWidth * widthper - mCenter2) <= mXRadius) {
                            xmove
                        } else {
                            (mCenter2 - mXRadius - startX1 + rainNum * strokeWidth * widthper).toFloat()
                        }
                    }
                }

                eventY = event.y
                val move = (event.y - mPreY)
                if (move != 0f) {

                    if (mPreX > mWidth / 2 || mPreX == (mWidth / 2).toFloat()) {
                        if (Math.abs((y1 - move)) < mYRadius) {
                            moveY = move
                        } else {
                            if (move > 0) {
                                moveY = mYRadius + y1
                            } else {
                                moveY = -mYRadius + y1
                            }
                        }

                    }
                    if (mPreX < mWidth / 2 && mPreX > 0 || mPreX == (mWidth / 2).toFloat()) {
                        if (Math.abs((y3 - move)) < mYRadius) {
                            moveY1 = move
                        } else {
                            if (move > 0) {
                                moveY1 = mYRadius + y3
                            } else {
                                moveY1 = -mYRadius + y3
                            }
                        }
                    }
                }



                isMove = true
                invalidate()

            }
            MotionEvent.ACTION_UP -> {
                endX = event.x
                endY = event.y
                startX = HorizonmMoveX + startX
                startX1 = startX1 + HorizonmMoveX1
                HorizonmMoveX = 0f
                HorizonmMoveX1 = 0f
                y1 = y1 - moveY
                y2 = y2 + moveY
                y3 = y3 - moveY1
                y4 = y4 + moveY1
                mPreX = 0f
                mPreY = 0f
                eventY = 0f
                moveY = 0f
                moveY1 = 0f
                moveX = 0f
                isMove = false
            }
        }
        return true
    }
}